// pages/home/home.js
const App = getApp();
const url = App.globalData.baseAPI;
var config = require("../../config.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {

    //用户登录的id号，也就是学号
    id:  config.userId,
    pageSize: 10,
    page: 1,
    pages: 0,
    swiperList: [
      {
        "id": 1,
        "image": "/image/img/w3.png"
      },
      {
      "id": 2,
      "image": "/image/img/w2.jpg"
    }],
    gridList: [],
    group: [{
      "id": "1",
      "name": "学校通告",
      "image": "../../icon/school.png",
      "type": "Ori",
      "color": "#FFFF99"
    },
    {
      "id": "2",
      "name": "社团通告",
      "image": "../../image/ico/社团活动.png",
      "type": "Tea",
      "color": "#CCFF99"
    },
    {
      "id": "3",
      "name": "失物招领",
      "image": "../../image/ico/试卷.png",
      "type": "Lun",
      "color": "#99CCFF"
    },
    {
      "id": "5",
      "name": "日常生活",
      "image": "../../image/ico/search.png",
      "type": "Found",
      "color": "#CC99FF"
    },
    ],
    message: []



  },

  // 获取Blog数据列表
  getBlogList() {
    this.setData({
      id:config.userId
    })
    // console.log(id)
    wx.request({
      url: url + '/blog/' + this.data.id + '/page?' + 'pageSize=' + this.data.pageSize + '&page=' + this.data.page+"&type=",

      method: 'GET',
      // data:{
      //   _page:this.data.page,
      //   _limit:this.data.pageSize
      // },
      success: (res) => {
        console.log(res.data)
        this.setData({
          message: [...this.data.message, ...res.data.data.records],
          pages: res.data.data.pages,
          // message:[res.data.data]
          page:this.data.page+1,
        })
      }
    })

  },
  //分享功能

  jumpToSearch() {
    // 页面跳转
    wx.navigateTo({
      url: '/pages/search/index'
    });
  },

  // 跳转到关注页面
  jumpToAttention() {
    // 页面跳转
    wx.navigateTo({
      url: '/pages/PageContant/attention?title=我的关注&stuId=' + this.data.id
    });
  },
  // 跳转到发布页面
  jumpToFabu() {
    // 页面跳转
    wx.navigateTo({
      url: '/pages/create/create'
    });
  },
  // 跳转到热点页面
  jumpTohotspot() {
    // 页面跳转
    wx.navigateTo({
      url: '/pages/PageContant/hotspot?title=今日热点'
    });
  },
  // 用户点击喜欢按钮，按钮颜色变化，like+1
  // 再次点击，按钮颜色变为灰色，
  // 从点赞数据表中寻找对应的用户，以及相应的博客，看一看是否已经点赞
  // 同理：关注也是如此
  likeadd(e) {
    console.log(e)
    console.log("列表位置：" + e.target.dataset.id)
    console.log("博客ID：" + this.data.message[e.target.dataset.id].blogId)
    var index = e.target.dataset.id;
    // console.log(e.target.dataset.blog.blogId);
    var num = this.data.message[index].likes;
    console.log("点赞数：" + num)
    //判断是否已经点赞
    // isshow = 1 : 已经点赞
    // isshow = 0 : 没有点赞
    var isshow = this.data.message[index].liked;
    // const blogId = this.data.message[id].blogId
    console.log("是否已经点赞：" + isshow)
    // 1、没有点赞
    if (isshow == 0) {
      console.log("没有点赞")
      wx.request({
        url: `http://localhost:8080/liked/${config.userId}/like?blogId=${this.data.message[index].id}`,
        method: 'GET',
          success: (res) => {
          this.setData({
            //得到新的数据
            // e.likes:20,
            [`message[${index}].likes`]: ++num,
            [`message[${index}].liked`]: 1,
            // likes:res.data
          })
          console.log(res.data)
        }
      })
    }

    //2.已经点赞
    else {
      console.log("已经点赞！")
      wx.request({
        url: `http://localhost:8080/liked/${config.userId}/likeDel?blogId=${this.data.message[index].id}`,
        method: 'GET',
        success: (res) => {
          // e.target.dataset.blog.likes = 20;
          this.setData({
            [`message[${index}].likes`]: --num,
            [`message[${index}].liked`]: 0,
            page:this.page+1
          })
          console.log(res.data)
          // e.target.dataset.blog.likes = 20;
        }
      })
    }
    //  this.setData({
    //   [`message[${id}].likes`]: num, //es6模板语法（反撇号字符）
    //   // [`message[${i}].favor_img`]: "../../image/favor.png",
    //  })

  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    console.log("home:onLoad");
    this.setData({
      id: config.userId,
    })
    this.getBlogList();
  },
  getSwiperList() {
    wx.request({
      url: 'https://www.escook.cn/slides',
      method: 'GET',
      success: (res) => {
        console.log(res),
          this.setData({
            swiperList: [...this.data.swiperList, ...res.data]
          })
      }
    })

  },
  //获取公告栏数据
  // getGridList() {
  //   wx.request({
  //     url: 'https://www.escook.cn/categories',
  //     method: "GET",
  //     success: (res) => {
  //       console.log(res)
  //       this.setData({
  //         gridList: res.data
  //       })
  //     }
  //   })
  // },
  // share(){

  //   // console.log(1)
  //   //判断触发的方式是否为按钮
  //   // if(res.from=="button"){
  //     // 参数
  //     console.log(1);
  //     let uid = "111";
  //     return{
  //       title:"标题",
  //       path:"/pages/dynamic/dynamic?uid="+uid
  //     }
  //   // }
  // },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {
    
      this.setData({
        page:0,
        message:[],
      })
      this.getBlogList()
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    //TODO 加载更多数据
    if (this.data.page < this.data.pages) {
      console.log("page:"+this.data.page);
      console.log("pages:"+this.data.pages);
      
      this.getBlogList();
    }else{
      console.log("page:"+this.data.page);
      console.log("pages:"+this.data.pages);
      wx.showToast({
        icon:"error",
        title: '没有更多数据了',
      })
    }

  },

  /**
   *  获得用户类型 
   */
  getUserType(type) {
      if (type == 0) {
        return "学生";
      } else if (type == 1) {
        return "教师";
      } else if (type == 2) {
        return "家长";
      } else if (type == 3) {
        return "管理员";
      }
  }
})